import Vue from 'vue'
import VueRouter from 'vue-router'
import { getToken, getRoles } from '@/utils/cookie'
// 登录注册页面
import Login from '@/views/Login/Login.vue'
// 首页页面
import Home from '@/views/Home/Home.vue'

Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err)
}

const routes = [
  {
    path: '/',
    redirect: '/home',
  },
  {
    path: '/login',
    component: Login,
    meta: '登录注册',
    redirect: '/login/LT',
    children: [
      {
        path: 'LT',
        name: 'loginTable',
        component: () => import('@/views/Login/Login&register/inLogin.vue'),
      },
      {
        path: 'RT',
        name: 'registerTable',
        component: () => import('@/views/Login/Login&register/inRegister.vue'),
      },
    ],
  },
  {
    path: '/home',
    component: Home,
    meta: '主页',
    children: [
      {
        path: '',
        name: 'index',
        component: () => import('@/views/Home/Index/index.vue'),
      },
      {
        path: 'admin-index',
        component: () => import('@/views/Home/Index/admin.vue'),
        children: [
          {
            path: '',
            component: () => import('@/views/Home/admin-page/index.vue'),
          },
          {
            path: 'health',
            name: 'health',
            meta: '健康上报',
            component: () => import('@/views/Home/admin-page/health.vue'),
          },
          {
            path: 'travel',
            name: 'travel',
            meta: '行程上报',
            component: () => import('@/views/Home/admin-page/travel.vue'),
          },
          {
            path: 'material',
            name: 'material',
            meta: '物资管理',
            component: () => import('@/views/Home/admin-page/material.vue'),
          },
          {
            path: 'voluntary-activity',
            name: 'activity',
            meta: '志愿活动',
            component: () =>
              import('@/views/Home/admin-page/Voluntary/activity.vue'),
          },
          {
            path: 'voluntary-feedback',
            name: 'feedback',
            meta: '志愿反馈',
            component: () =>
              import('@/views/Home/admin-page/Voluntary/feedback.vue'),
          },
          {
            path: 'userManagement',
            name: 'userManagement',
            meta: '人员管理',
            component: () => import('@/views/Home/admin-page/user.vue'),
          },
          {
            path: 'helpManagement',
            name: 'helpManagement',
            meta: '求助管理',
            component: () => import('@/views/Home/admin-page/help.vue'),
          },
          {
            path: 'notice',
            name: 'notice',
            meta: '通知管理',
            component: () => import('@/views/Home/admin-page/notice.vue'),
          },
          {
            path: 'auth',
            name: 'auth',
            meta: '权限管理',
            component: () => import('@/views/Home/admin-page/auth.vue'),
          },
        ],
      },
      {
        path: 'upload',
        name: 'upload',
        meta: '上报',
        component: () => import('@/views/Home/Upload/Upload.vue'),
      },
      {
        path: 'healthTabel/:id',
        name: 'healthTabel',
        meta: '健康上报表',
        component: () => import('@/views/Home/UploadTable/HealthTable.vue'),
      },
      {
        path: 'travelTabel/:id',
        name: 'travelTabel',
        meta: '行程上报表',
        component: () => import('@/views/Home/UploadTable/TravelTabel.vue'),
      },
      {
        path: 'user',
        name: 'user',
        meta: '个人中心',
        component: () => import('@/views/Home/User/User.vue'),
      },
      {
        path: 'message',
        name: 'message',
        meta: '消息',
        component: () => import('@/views/Home/Message/Message.vue'),
      },
      {
        path: 'community',
        name: 'community',
        meta: '居民求助',
        component: () => import('@/views/Home/Community/Community.vue'),
      },
    ],
  },
  {
    path: '/401',
    component: () => import('@/views/error/401.vue'),
  },
]

const router = new VueRouter({
  routes,
})

// let adminPromission = ''

// router.beforeEach((to, from, next) => {
//   const { path } = to
//   // 判断是否登录
//   if (!getToken()) {
//     switch (path) {
//       case '/login/LT':
//         return next()
//       case '/login/RT':
//         return next()
//       default:
//         return next('/login/LT')
//     }
//   } else {
//     if (path === '/login/LT' || path == '/login/RL') next('/')
//     if (getRoles() !== 'ADMIN' && path.search('/home/admin-index') !== -1) {
//       next('/401')
//       // console.log('go admin-index')
//     } else {
//       // console.log('go index')
//       next()
//     }
//   }
//   // next()
// })
export default router
